//package com.ww.httpclient;
//
//import cn.hutool.json.JSONUtil;
//import lombok.Data;
//import lombok.experimental.Accessors;
//import org.apache.hc.client5.http.classic.methods.HttpGet;
//import org.apache.hc.client5.http.classic.methods.HttpPost;
//import org.apache.hc.client5.http.config.ConnectionConfig;
//import org.apache.hc.client5.http.config.RequestConfig;
//import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
//import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
//import org.apache.hc.core5.http.Header;
//import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
//import org.apache.hc.client5.http.impl.classic.HttpClients;
//import org.apache.hc.core5.http.ContentType;
//import org.apache.hc.core5.http.HttpHeaders;
//import org.apache.hc.core5.http.HttpStatus;
//import org.apache.hc.core5.http.io.entity.StringEntity;
//import org.apache.hc.core5.http.message.BasicHeader;
//import org.apache.hc.core5.http.message.BasicNameValuePair;
//import org.apache.hc.core5.net.URIBuilder;
//import org.apache.hc.core5.util.Timeout;
//
//import java.io.IOException;
//import java.net.URI;
//import java.net.URISyntaxException;
//import java.net.URL;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//public class HttpClientDemo {
//
//    public static void main(String[] args) throws IOException, URISyntaxException {
//        // 获取默认配置的HttpClient
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//
//        // 根据系统配置创建HttpClient
//        CloseableHttpClient httpClient1 = HttpClients.createSystem();
//
//        // 自定义创建
//        HttpClients.custom()
//                .setDefaultHeaders(Collections.emptyList()) // 设置默认请求头
//                .setDefaultRequestConfig(RequestConfig.DEFAULT) // 设置默认配置
//                .build();
//
//        /**
//         * 在项目启动时可以通过设置如下JVM启动参数
//         * http.agent配置userAgent
//         * http.keepAlive配置keepAlive数据
//         */
//
//        /**
//         * 配置参数
//         * =================================
//         */
//
//        // 在创建HttpClient对象时设置全局配置，也可以为单个请求设置请求配置
//        RequestConfig requestConfig = RequestConfig.custom()
//                // 过时方法，推荐下边写法
//                .setConnectTimeout(Timeout.of(3000, TimeUnit.MICROSECONDS))
//                .setResponseTimeout(3000, TimeUnit.MICROSECONDS)
//                .setConnectionRequestTimeout(3000, TimeUnit.MICROSECONDS)
//                .build();
//
//        ConnectionConfig connectionConfig = ConnectionConfig.custom()
//                .setConnectTimeout(Timeout.of(3000, TimeUnit.MICROSECONDS))
//                .build();
//
//        // 单个请求设置配置
//        String url = "";
//        HttpGet httpGet = new HttpGet(url);
//        httpGet.setConfig(requestConfig);
//
//
//        // ===========================================设置公共请求头============================================
//        List<Header> headers = new ArrayList<>();
//        headers.add(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON));
//        headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, x-gzip, deflate"));
//        headers.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
//
//        CloseableHttpClient httpClient2 = HttpClients.custom()
//                .setDefaultHeaders(headers)
//                .build();
//
//
//        // ===========================================Get请求============================================
//        /**
//         * Get请求的所有参数都是直接拼接在URL后面的，在HttpClient中有两种方式可以实现
//         */
//        // 1、请求参数直接拼接在请求路径后面
//        String name = URLEncoder.encode("张三", "utf-8");
//        String url2 = "http://localhost:10010/user/params?age=20&name=" + name;
//        // 创建GET请求对象
//        HttpGet httpGet2 = new HttpGet(url2);
//        // 调用HttpClient的execute方法执行请求
//        CloseableHttpResponse response = httpClient.execute(httpGet2);
//        // 获取请求状态
//        int code = response.getCode();
//        if (code == HttpStatus.SC_OK) {
//            System.out.println("请求响应成功");
//        }
//        // 2、通过URIBuilder构建请求路径
//        URL url3 = new URL("http://localhost:8080/...");
//        URI uri = new URIBuilder()
//                .setScheme(url3.getProtocol())
//                .setHost(url3.getHost())
//                .setPort(url3.getPort())
//                .setPath(url3.getPath())
//                .setParameters(
//                        new BasicNameValuePair("name", "张三"),
//                        new BasicNameValuePair("age", "20")
//                )
//                .build();
//        HttpGet httpGet3 = new HttpGet(uri);
//        CloseableHttpResponse httpResponse = httpClient.execute(httpGet3);
//        int code1 = httpResponse.getCode();
//        if (code1 == HttpStatus.SC_OK) {
//            System.out.println("请求响应成功");
//        }
//
//
//        // ===========================================Post请求============================================
//        // 1、JSON
//        // 在HttpClient中POST请求发送数据是通过调用HttpPost类的setEntity(HttpEntity entity)方法设置消息内容的
//        /**
//         * 发送JSON
//         * HttpClient中发送JSON数据可以使用StringEntity类实现
//         */
//        String url4 = "http://localhost:8080/...";
//        // 创建post请求对象
//        HttpPost httpPost = new HttpPost(url4);
//        // 构建对象
//        User user = new User();
//        user.setName("张三")
//                .setAge(20)
//                .setAddress("中国深圳")
//                .setCity("深圳")
//                .setCountry("中国")
//                .setAihao(Arrays.asList("香山", "香格里拉"));
//
//        // 创建字符串实体对象
//        StringEntity entity = new StringEntity(JSONUtil.toJsonStr(user));
//        httpPost.setEntity(entity);
//
//        // 发送POST请求
//        httpClient.execute(httpPost);
//
//        // 2、提交表单数据
//        // 创建ContentType对象为form表单模式
//        ContentType contentType = ContentType.create("application/x-www-form-urlencoded", StandardCharsets.UTF_8);
//        // 添加到HttpPost头中
//        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
//        // 创建entity
//        // 方式1：自己拼接请求数据，并且创建StringEntity对象
//        String query = "name=" + URLEncoder.encode("张三", "utf-8") + "&age=20";
//        StringEntity entity1 = new StringEntity(query);
//
//        // 方式2：通过UrlEncodeFormEntity创建HttpEntity
//        UrlEncodedFormEntity httpFormEntity = new UrlEncodedFormEntity(
//                Arrays.asList(new BasicNameValuePair("name", "张三"),
//                        new BasicNameValuePair("age", "20")),
//                StandardCharsets.UTF_8);
//        // 把HttpEntity设置到HttpPost中
//        httpPost.setEntity(httpFormEntity);
//    }
//}
//
//@Data
//@Accessors(chain = true)
//class User {
//    private String name;
//    private Integer age;
//    private String address;
//    private String country;
//    private String city;
//    private List<String> aihao;
//}